<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Building controller
 */
class Upload extends Api_Controller
{
    /**
     * @apiDefine Upload 通用接口
     */
    function __construct()
    {
        // Construct the parent class
        parent::__construct();
    }

    /**
     * @api               {get} upload/index 图片上传接口
     * @apiGroup          Upload
     * @apiName           upload/index
     * @apiVersion        1.0.0
     * @apiDescription    图片上传接口
     * @apiSampleRequest http://app.91zoy.com/api/upload/index
     * @apiHeader {String} Authorization {basic Authorization}
     * @apiParam {file} file 图片【常规form-data方式，name=file】
     * @apiParam {String} type 图片类型 【car, user, order,other】
     * @apiSuccessExample 成功响应
     *          {
     *           "status": 1",
     *           "message": "ok",
     *           "code": 200,
     *           "data": []
     *          }
     * @apiErrorExample   失败响应
     *            {
     *             "status": 0,
     *             "message": "error",
     *             "code": 0,
     *             "data": []
     *            }
     */
    public function index_post()
    {
        $type = trim($this->post('type'));
        if (empty($type)) {
            $this->failure([], "缺少图片类型参数");
        }
        if (!in_array($type, $this->config->item('image_type'))) {
            $this->failure([], "图片类型不符合要求");
        }
        if ($_FILES) {
            $config['upload_path']   = './uploads/api/' . $type;
            $config['allowed_types'] = '*';
            $config['encrypt_name']  = true;
            $config['max_size']      = '500000';
            if (!file_exists($config['upload_path'])) {
                mkdir($config['upload_path'], 0777, true);
            }
            if ($_FILES['file']['name'] == '') {
                $this->failure([], "请选择图片");
            }
            $allowed_types = ['jpg', 'jpeg', 'png', 'bmp', 'gif'];
            $torrent       = explode(".", $_FILES['file']['name']);
            $fileend       = strtolower(end($torrent));
            if (!in_array($fileend, $allowed_types)) {
                $this->failure([], "图片类型不正确");
            }
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('file')) {
                $this->failure([], "头像上传失败");
            } else {
                //upload sucess
                $data = $this->upload->data();
                if (!empty($data['full_path'])) {
                    $this->success([
                        'url'        => base_url('uploads/api/' . $type . "/" . $data['file_name']),
                        'path'       => 'uploads/api/' . $type . "/" . $data['file_name'],
                        'image_type' => $data['image_type'],
                        'orig_name'  => $data['orig_name'],
                    ]);
                } else {
                    $this->failure([], "图片设置错误");
                }
            }
        } else {
            $this->failure([], "请选择图片");
        }
    }

}